package com.wenhai.spring.resource;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.PathMatcher;

import java.io.IOException;

/**
 * 自定义 {@link ResourcePatternResolver} 示例
 *
 * @author wenhai
 * @date   2021/4/7
 * @see ResourcePatternResolver
 * @see PathMatchingResourcePatternResolver
 * @see PathMatcher
 */
public class CustomizedResourcePatternResolverDemo {
    public static void main(String[] args) throws IOException {

        System.out.println(System.getProperties());
        String currentPackagePath = System.getProperty("user.dir") + "\\resource\\src\\main\\java\\com\\wenhai\\spring\\resource\\";
        String locationPattern = currentPackagePath + "*.java";
        PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(new FileSystemResourceLoader());
        Resource[] resources = resourcePatternResolver.getResources(locationPattern);
        for (Resource resource : resources) {
            EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
            System.out.println(IOUtils.toString(encodedResource.getReader()));
        }
    }
}
